<?php
/**
 * Created by PhpStorm.
 * User: yuhonghui
 * Date: 2018/3/14
 * Time: 下午12:10
 */

namespace frontend\controllers;

use common\models\ar\MoneyRecharge;
use common\models\ar\SysConfig;
use common\models\ar\UserMoneyChangerecord;
use common\models\ar\ChangeCache;
use common\models\ar\ExchangeRate;
use common\models\MoneyDao;
use common\models\Pay;
use common\models\ar\UserMoney;
use yii\web\Controller;
class PayReturnController extends Controller
{
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        // $req=file_get_contents("php://input");
        // $file=fopen("D:/http/Apache24/htdocs/pay/common/config/1.txt","w");
        // fwrite($file,$req);
        // fclose($file);
        // exit();
        $req = $_POST;
        $rmb=ExchangeRate::findone(['currency'=>'RMB']);
        $result=$req['status'];
        if ($result=="成功") {
        $recharge_sn = $req['businessnumber'];
        $tmp = MoneyRecharge::findOne(['recharge_sn' => $recharge_sn]);
        $time = time();
        if ($tmp) {
            if (empty($tmp->data_response)) {
               if ($result=="成功") {
                $tmp->status = 2; // 支付成功
                $tmp->pay_end_time = $time;
                $tmp->data_response = json_encode($req['remark']);
                $tmp->save();
                $Tmoney=$tmp->money;
                //$Tmoney=($Tmoney-$Tmoney*0.005);
                $Hmoney=($Tmoney-$Tmoney*0.006)/$rmb['exchange_rate'];
                    $Zmoney=UserMoney::findone(['userID'=>$tmp->userID]);
                    $a=(float)($Zmoney->totalMoney);
                    $c=(float)($Zmoney->userDeposit);
                    $b=(float)$Hmoney;
                    $b=sprintf("%.2f",substr(sprintf("%.3f",$b), 0,-1));
                    $money=$a+$b;
                    $userMoney=$c+$b;
                    $Zmoney->totalMoney=$money;
                    $Zmoney->userDeposit=$userMoney;
                    $Zmoney->save();
                    $Money_Record = new UserMoneyChangerecord;
                    $Money_Record->userID=$tmp->userID;
                    $Money_Record->changeMoney=$b;
                    $Money_Record->changeDesc="入劣后:三方支付";
                    $Money_Record->changeTime=Date('Y-m-d H:i:s');
                    $Money_Record->type=1;
                    $Money_Record->tradeId=0;
                    $Money_Record->save();
                    $sql="SELECT id FROM `user_money_changerecord` WHERE 
                  changeTime=(SELECT MAX(changeTime) FROM `user_money_changerecord` WHERE `userID`={$tmp->userID})";
                    $extra_id=$Money_Record::findBySql($sql)->all();
                    $Change_Record = new ChangeCache;
                    $Change_Record->userid=$tmp->userID;
                    $Change_Record->change_type=2;
                    $Change_Record->extra_id=$extra_id;
                    $Change_Record->save();
                    echo "SUEESS";
              }
              else {
                $tmp->status = 3; // 支付失败
                $tmp->pay_end_time = $time;
                $tmp->data_response = json_encode($result['body']);
                $tmp->save();
                }
             } 
           }
          }
    }
}
